package com.imooc.newretail.bpl.ctrl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imooc.newretail.common.BaseController;
import com.imooc.newretail.common.utils.BeanUtils;
import com.imooc.newretail.common.vo.RV;
import com.imooc.newretail.common.vo.ResultVO;
import com.imooc.newretail.bpl.dto.DeptDTO;
import com.imooc.newretail.bpl.dto.UserDTO;
import com.imooc.newretail.bpl.entity.User;
import com.imooc.newretail.bpl.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 系统用户信息表 前端控制器
 * </p>
 *
 * @author Hank
 * @since 2020-06-27
 */
@RestController
@RequestMapping("/sys/user")
public class UserController extends BaseController<IUserService> {

    @Autowired
    IUserService userService;

    /***
     * 新增和修改部门对象
     * @param userDTO
     * @return
     */
    @PostMapping("/")
    public ResultVO save(@RequestBody UserDTO userDTO) {
        User user = BeanUtils.copyProperties(userDTO, User.class);
        boolean dotype = userService.saveOrUpdate(user);
        return dotype ? RV.success(dotype) : RV.fail("操作失败");
    }

    @DeleteMapping("/{userId}")
    public ResultVO deleteById(@PathVariable("userId") String userId) {
        boolean dotype = userService.removeUser(userId);
        return dotype ? RV.success(dotype) : RV.fail("操作失败");
    }

    @GetMapping("/{userId}")
    public ResultVO<DeptDTO> getDeptsByTenantId(@PathVariable("userId") String userId) {
        return RV.success(userService.getById(userId));
    }

    @GetMapping("/list")
    public ResultVO<UserDTO> findList(@RequestParam("currentPage") Integer cuurentPage, @RequestParam("pageRow") Integer pageRow) {
        QueryWrapper query = new QueryWrapper();
        query.orderByAsc("create_time");
        return RV.success(userService.page(new Page<>(cuurentPage, pageRow), query));
    }
}
